//列表请求地址
var url=global_param.context_name+"/song/getMusicList";
var pageNumber=1,pageSize=10;
$(function() {		
	initTable();
	//点击选中行，改变选中行的背景颜色
	$("#notMusic").on('click-row.bs.table', function (e, row, element){}); 
	typeSelect();
	initPage();
});

function typeSelect (){
	$.ajax({
    	async: false,
    	url : global_param.context_name + '/song/getMusicType', 
		type : 'post',  				    
		success: function(data){			
			var output = [];
			output.push("<option value=''>请选择</option>");
			$.each(data, function(key, value)
			{
				output.push('<option value="'+ value.KEY +'">'+ value.value +'</option>');
			});
			
			$('#music_type').html(output.join(''));  
		}  
  	});
}

function initPage(){
		
    //日期初始化
    $('#create_timestamp').daterangepicker(globalUtil.initinfo);
    $('#create_timestamp').val("");

    /* 回显标识true 初始化筛选项 */
    if(temp_param_flag){
    	initParams(global_temp_param);
    	global_temp_param = {};
    	temp_param_flag = false;
    }
    //初始化表格
	initTable();	 
}


//清空查询条件
function clearMusic()
{
    $("#music_name").val("");
    $("#sing_personnel").val("");    
 	$("#create_timestamp").val("");
    $("#music_type").val("");
    $('#creat_user_name').val("");
    $('#music_state').val("");
    $('#music_source').val("");
}

function searchMusic()
{
	$('#notMusic').bootstrapTable('refresh',{url: url,pageNumber:pageNumber,pageSize:10});
}

function refreshMusic()
{
	$('#notMusic').bootstrapTable('refresh');
}

//刷新列表
function queryParams()
{
 	$('#notMusic').bootstrapTable('refresh',{url: url});
}

function initTable(){
	var columns = [
    {
        field: '',
        title: '序号',
        switchable:false,
    	align:"center", 
    	valign:"middle",
        formatter: function (value, row, index) {
                return index+1;
        }
    },
    {
    	 field : 'music_code',
         v_field : 'music_code',
         title : '歌曲编号',
         align : 'center',
         valign : 'middle'
    },
    {
        field : 'creat_user_id',
        v_field : 'creat_user_id',
        title : '创建人id',
        align : 'center',
        valign : 'middle',
        visible: false
    },
    {
        field : 'music_name',
        v_field : 'music_name',
        title : ' 歌曲名称 ',
        align : 'center',
        valign : 'middle'
    }/*, {
        field : 'music_type_str',
        v_field : 'music_type_str',
        title : '歌曲类型',
        align : 'center',
        valign : 'middle'
    }*/, {
        field : 'sing_personnel',
        v_field : 'sing_personnel',
        title : '演唱人',
        align : 'center',
        valign : 'middle'
    },{
        field : 'creat_user_name',
        v_field : 'creat_user_name',
        title : '创建人',
        align : 'center',
        valign : 'middle',
    },{
        field : 'music_source_str',
        v_field : 'music_source_str',
        title : '歌曲来源',
        align : 'center',
        valign : 'middle'
    }, {
        field : 'create_timestamp_str',
        v_field : 'create_timestamp_str',
        title : '创建时间',
        align : 'center',
        valign : 'middle'
    }, {
        field : 'music_state',
        v_field : 'music_state',
        title : '歌曲状态',
        align : 'center',
        valign : 'middle',
        formatter:function(value, row, index)
        {
        	//0:待发布,1:待审核,2:未通过,3:待发布
        	if(value == '0'){
        		return '待发布';
        	}else if(value == '1'){
        		return '待审核';
        	}else if(value == '2'){
        		return '未通过';
        	}else if(value == '3'){
        		return '待发布';
        	}
        }
    },{
        field : 'action',
        title : '操作',
        align : 'center',
        valign : 'middle',
        switchable:false,
        clickToSelect:false,
        searchable:false,
        formatter:function(value, row, index)
        {
        	var html = "";
        	//查看
        	html += '<a href="javascript:showMusicInfo('+row.music_id+');" class="iconbtn btn-blue tooltip-test" data-toggle="tooltip" title="查看"><i class="zxfont icon-search"></i></a>';
        	if(row.music_state == '3' && row.music_source_str == '后台'){
        		//编辑
        		html += '<a href="javascript: updMusicInfo('+row.music_id+');" class="iconbtn btn-blue tooltip-test" data-toggle="tooltip" title="编辑"><i class="zxfont icon-edit"></i></a>';
        		//删除
        		html += '<a href="javascript: delMusicInfo('+row.music_id+');" class="iconbtn btn-red tooltip-test" data-toggle="tooltip" title="删除"><i class="zxfont icon-trash"></i></a>';
        		//发布
        		html += '<a href="javascript:submitMusicInfo('+row.music_id+',\''+row.music_name+'\',\''+row.music_source_str+'\','+row.creat_user_id+',\''+row.creat_user_name+'\');" class="iconbtn btn-blue tooltip-test" data-toggle="tooltip" title="发布"><i class="fa fa-share"></i></a>';
        	}
        	
        	if(row.music_state == '1'){
        		//审核										  
        		html += '<a href="javascript:AuditingMusicInfo('+row.music_id+',\''+row.music_name+'\','+row.creat_user_id+',\''+row.creat_user_name+'\');" class="iconbtn btn-blue tooltip-test" data-toggle="tooltip" title="审核"><i class="zxfont icon-verify"></i></a>';
        	}
        	
        	if(row.music_state == '2'){
        		//删除
        		html += '<a href="javascript: delMusicInfo('+row.music_id+');" class="iconbtn btn-red tooltip-test" data-toggle="tooltip" title="删除"><i class="zxfont icon-trash"></i></a>';
        	}
        	
        	if(row.music_state == '3' && row.music_source_str == 'app'){
        		//删除
        		html += '<a href="javascript: delMusicInfo('+row.music_id+');" class="iconbtn btn-red tooltip-test" data-toggle="tooltip" title="删除"><i class="zxfont icon-trash"></i></a>';
        		//发布
        		html += '<a href="javascript:submitMusicInfo('+row.music_id+',\''+row.music_name+'\',\''+row.music_source_str+'\','+row.creat_user_id+',\''+row.creat_user_name+'\');" class="iconbtn btn-blue tooltip-test" data-toggle="tooltip" title="发布"><i class="fa fa-share"></i></a>';
        	}
        	
        	return html;
        }
    }
    ];
    $('#notMusic').bootstrapTable({
    	  method:'POST',
          dataType:'json',
          toolbar:"#toolbar",
          contentType: "application/x-www-form-urlencoded",
          url:url,
          cache: false,
          striped: true,                              //是否显示行间隔色
          sidePagination: "server",           	//分页方式：client客户端分页，server服务端分页（*）
          pagination:true,
          minimumCountColumns:2,
          pageNumber:pageNumber,                       //初始化加载第一页，默认第一页
          pageSize: pageSize,    //每页的记录行数（*）
          sortable: true, //是否启用排序
          sortName:'create_timestamp',
          sortOrder:'desc',
          pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
          uniqueId: "music_id",              //每一行的唯一标识，一般为主键列
          showExport: true,                    
          exportDataType: 'all',
          queryParamsType:"",
          showRefresh:false,					//是否显示刷新按钮
          search:false,						//是否显示搜索框
          showToggle:false,
          ajaxOptions: {traditional: true},
          clickToSelect: true,
	      onLoadSuccess: function() {
	    	  initTabTable($("#notMusic"));
	      },
          onColumnSwitch:function(){
        	initTabTable($("#notMusic"));
          },
          onClickCell:function(field,value,row){
        	if(field == "action"){
        		return;
        	}
          },
        queryParams:function queryParams(params) { //设置查询参数  
	        var param = {    
		        page: params.pageNumber,    
		        pagesize: params.pageSize,
		        sortname:params.sortName,
		        sortorder:params.sortOrder,
			    sing_personnel:$("#sing_personnel").val(),
			    music_name:$("#music_name").val(),
			    music_type:$("#music_type").val(),
			    creat_user_name:$("#creat_user_name").val(),
			    create_timestamp:$("#create_timestamp").val(),
			    music_state:$("#music_state").val(),
			    music_source:$("#music_source").val()
	        };
	        global_temp_param = param;
	        return param;                   
        },
        columns: columns
    });
}

//新增修改公用一个dialog
var addOrEditMusicInfoDialog;
function addMusic(){
	addOrEditMusicInfoDialog = BootstrapDialog.show({												
        message: $('<div id="addDiv class="modal-dialog" ></div>').load('static/html/musicManage/addOrEditMusic.html'),
        title: '歌曲信息',
        titleClass: 'text-center',
        closable: false,
        size:BootstrapDialog.SIZE_MIDDLE,
        data : { "flag_one" : "add" },
        buttons: [
        	{
                label: '保存',
                cssClass:'btn btn-primary col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
                action: function(dialog) {
                	musicSave(dialog);
                }
            },
        	{
            label: '关闭',
            cssClass:'btn btn-default col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
            action: function(dialog) {
            	$("#addDiv").html("");
                dialog.close();
            }
        }]
   });	
}

function updMusicInfo(music_id){
	addOrEditMusicInfoDialog = BootstrapDialog.show({												
        message: $('<div id="updDiv class="modal-dialog" ></div>').load('static/html/musicManage/addOrEditMusic.html'),
        title: '歌曲信息',
        titleClass: 'text-center',
        closable: false,
        size:BootstrapDialog.SIZE_MIDDLE,
        data:{'music_id':music_id,"flag_one" : "edit"},
        buttons: [
        	{
                label: '保存',
                cssClass:'btn btn-primary col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
                action: function(dialog) {
                	musicUpdSave(dialog);
                }
            },
        	{
            label: '关闭',
            cssClass:'btn btn-default col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
            action: function(dialog) {
            	$("#updDiv").html("");
                dialog.close();
            }
        }]
   });	
}

//删除
function delMusicInfo(music_id){
	globalUtil.showConfirmMessage("确定删除所选歌曲？",function(){
		$.ajax({
	    	async: false,
	    	url : global_param.context_name + '/song/delMusicById', 
			type : 'post', 
			data : { "music_id" : music_id },
			success: function(data){			
				if(data == 'success'){
					globalUtil.showSuccessMessage('删除成功');
				}else{
					globalUtil.showErrorMessage('删除失败');
				}
				refreshMusic();
			}  
	  	});
	});
}

//发布
function submitMusicInfo(music_id,music_name,music_source_str,creat_user_id,creat_user_name){
	globalUtil.showConfirmMessage("确定发布所选歌曲？",function(){
		$.ajax({
	    	async: false,
	    	url : global_param.context_name + '/song/updMusicById', 
			type : 'post', 
			data : { "music_id" : music_id ,"music_state" : 4, "flag" : "submit","music_name":music_name,"music_source_str":music_source_str,"creat_user_id":creat_user_id,"creat_user_name":creat_user_name},
			success: function(data){			
				if(data == 'success'){
					globalUtil.showSuccessMessage('操作成功！');
				}else{
					globalUtil.showErrorMessage('操作失败！');
				}
				refreshMusic();
			}  
	  	});
	});
}

//查看
var showMusicInfoDialog;
function showMusicInfo(music_id){
	showMusicInfoDialog = BootstrapDialog.show({												
        message: $('<div id="mendDiv class="modal-dialog" ></div>').load('static/html/musicManage/LookAndEdit.html'),
        title: '歌曲信息',
        titleClass: 'text-center',
        closable: false,
        size:BootstrapDialog.SIZE_MIDDLE,
        data:{'music_id':music_id},
        buttons: [
        	{
                label: '试听歌曲',
                cssClass:'btn btn-primary col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
                action: function(dialog) {
                	audition();
                }
            },
        	{
            label: '关闭',
            cssClass:'btn btn-default col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
            action: function(dialog) {
            	$("#mendDiv").html("");
                dialog.close();
            }
        }]
   });	
}


//审核
function AuditingMusicInfo(music_id,music_name,creat_user_id,creat_user_name){
	showMusicInfoDialog = BootstrapDialog.show({												
        message: $('<div id="auditingDiv class="modal-dialog" ></div>').load('static/html/musicManage/LookAndEdit.html'),
        title: '歌曲信息',
        titleClass: 'text-center',
        closable: false,
        size:BootstrapDialog.SIZE_MIDDLE,
        data:{'music_id':music_id},
        buttons: [
        	{
                label: '试听歌曲',
                cssClass:'btn btn-primary col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
                action: function(dialog) {
                	audition();
                }
            },
            {
                label: '通过',
                cssClass:'btn btn-primary col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
                action: function(dialog) {
                	pass(music_id,showMusicInfoDialog,music_name,creat_user_id,creat_user_name);
                }
            },
            {
                label: '不通过',
                cssClass:'btn btn-default col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
                action: function(dialog) {
                	noPass(music_id,showMusicInfoDialog,music_name,creat_user_id,creat_user_name);
                }
            },
        	{
            label: '关闭',
            cssClass:'btn btn-default col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
            action: function(dialog) {
            	$("#mendDiv").html("");
                dialog.close();
            }
        }]
   });	
}

function pass(music_id,showMusicInfoDialog,music_name,creat_user_id,creat_user_name){	
	globalUtil.showConfirmMessage("确定通过这首歌曲？",function(){
		$.ajax({
		   	data : {
		   		music_id : music_id,
		   		"flag" : "Auditing",
		   		"music_state" : 3,
		   		"music_name" : music_name,
		   		"creat_user_id" : creat_user_id,
		   		"creat_user_name" : creat_user_name
		   	},
		   	async: false,
		   	url : global_param.context_name + "/song/updMusicById", 
			type : 'post',  				    
			success: function(data){
				if(data == 'success'){
					globalUtil.showSuccessMessage('操作成功！');
				}else{
					globalUtil.showErrorMessage('操作失败');
				}
				showMusicInfoDialog.close();
				refreshMusic();
			}  
	  	}); 
	});
}

function noPass(music_id,showMusicInfoDialog,music_name,creat_user_id,creat_user_name){
	BootstrapDialog.show({
        title: ' 输入不通过原因，并确定',
        message: '<div id="auditingDiv class="modal-dialog" ><div class="col-md-12 a"><textarea id="Auditing" class="form-control"></textarea></div></div>',
        titleClass: 'text-center',
        closable: false,
        buttons: [
        	{
                label: '确定',
                cssClass:'btn btn-primary col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
                action: function(dialog) {
                	if($('#Auditing').val() == null || $('#Auditing').val() == ''){
                		globalUtil.showSuccessMessage("请输入不通过原因");
                		return false;
                	}
                	$.ajax({
            		   	data : {
            		   		music_id : music_id,
            		   		"flag" : "Auditing",
            		   		"music_state" : 2,
            		   		"Auditing" : $('#Auditing').val(),
            		   		"music_name" : music_name,
            		   		"creat_user_id" : creat_user_id,
            		   		"creat_user_name" : creat_user_name
            		   	},
            		   	async: false,
            		   	url : global_param.context_name + "/song/updMusicById", 
            			type : 'post',  				    
            			success: function(data){
            				if(data == 'success'){
            					globalUtil.showSuccessMessage('操作成功！');
            				}else{
            					globalUtil.showErrorMessage('操作失败');
            				}
            				dialog.close();
            				showMusicInfoDialog.close();
            				refreshMusic();
            			}  
            	  	}); 
                }
            },
        	{
            label: '关闭',
            cssClass:'btn btn-default col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
            action: function(dialog) {
                dialog.close();               
            }
        }]
    });	
}
